package com.healthy.controller.wechat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.healthy.entity.YgColumn;
import com.healthy.entity.YgDocument;
import com.healthy.entity.YgUsers;
import com.healthy.service.YgColumnService;
import com.healthy.service.YgUsersService;
import com.healthy.util.Dateutil;
import com.healthy.util.JWTUtil;


/**
 * controller 控制层的实现
 * @author HGB丶阳光
 * 移动端-> 专栏模块 YgColumnController
 */

@Controller
public class YgColumnController {

	// 注入Service层
	@Autowired
	private YgColumnService columnService;
	
	// 注入Service层
	@Autowired
	private YgUsersService usersService;
	
	/*
	 *  移动端：专栏模块功能一、用户添加专栏
	 */
	@RequestMapping(value="/c/addcolumn" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject addColumn(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 获取用户生成专栏 名称
		String name = (String) map.get("name");
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "专栏添加失败";
		int status = 1;
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id");
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 判断当前用户添加的专栏名称是否已存在
			List<YgColumn> results = this.columnService.findColumnByName(uid,name);
			System.out.println("results测试："+results);
			if(results.size() == 0){
				// 执行专栏添加方法
				boolean result = this.columnService.addColumn(uid, name);
				if(result != false){
					msg = "专栏添加成功";
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}else{
					msg = "专栏添加失败";
					status = -1;
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}
			}else{
				msg = "该专栏已存在";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能二、用户查看自己全部专栏，以及该专栏下的全部收藏的文章
	 */
	@RequestMapping(value="/c/findcolumnall" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject findColumnAll(HttpServletRequest request){
		// 测试map
//		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "专栏查看失败";
		int status = 1;
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id");
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 执行专栏查询方法
	//		List<YgUsers> user = this.usersService.findColumnAll(uid);
			List<YgColumn> column = this.columnService.allColumn(uid);
			System.out.println("测试list——columns:"+column);
			if(column != null){
				// 返回当前用户基本信息给前端,将信息封装进map集合中
//				Map<String,Object> users = new HashMap<>();
//				users.put("nickname",user.getNickname());
//				users.put("ygColumn",user.getYgColumn());
				// 通过 Set集合无序、不重复 来进行 筛选专栏名称重复的name值
//				Set<String> sets = new HashSet<String>();
//				for(String column : columns){
//					sets.add(column);
//				}
				msg = "用户专栏查看成功";
				json.put("msg", msg);
				json.put("column",column);
				json.put("status", status);
				return json;
			}else{
				msg = "用户专栏查看失败";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能三、用户查看某个专栏下的全部文章（显示所有收藏）
	 */
	@RequestMapping(value="/c/finduserbycolumn" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject findUserByColumn(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "专栏文章查看失败";
		int status = 1;
		// 获取当前需要查询的专栏名称
		String name = (String) map.get("name");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id"); 
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 查询某个专栏下的全部文章方法
			List<YgColumn> column = this.columnService.findUserByColumn(name);
			System.out.println("column测试："+column);
			if(column != null){
				// 返回当前用户基本信息给前端,将信息封装进map集合中
//				Map<String,Object> columns = new HashMap<>();
//				column.put("nickname",column.getNickname());
//				column.put("ygColumn",column.getYgColumn());
				msg = "专栏查看文章成功";
				json.put("msg", msg);
				json.put("column", column);
				json.put("status", status);
				return json;
			}else{
				msg = "该专栏下未收藏文章";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能四、用户修改文章所属的专栏
	 */
	@RequestMapping(value="/c/updatedocumentbycolumn" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateDocumentByColumn(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "文章所属的专栏修改失败";
		int status = 1;
		// 获取当前需要更改的专栏 名称、和 文章did
		String name = (String) map.get("name");
		String new_name = (String) map.get("new_name");
		String did = (String) map.get("did");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id"); 
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 修改文章所属的专栏方法
			boolean column = this.columnService.updateDocumentByColumn(name,new_name,did);
			System.out.println("column测试："+column);
			if(column != false){
				// 返回当前用户基本信息给前端,将信息封装进map集合中
//				Map<String,Object> columns = new HashMap<>();
//				column.put("nickname",column.getNickname());
//				column.put("ygColumn",column.getYgColumn());
				msg = "文章所属的专栏修改成功";
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}else{
				msg = "修改专栏不存在";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能五、用户删除专栏 , 通过前端传输过来的专栏  uid 或者   专栏名称 进行专栏的删除（uid、专栏名称不能重复，是唯一标识符）
	 */
	@RequestMapping(value="/c/deletedocumentbycolumn" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject deleteDocumentByColumn(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "专栏删除失败";
		int status = 1;
		// 获取当前需要更改的专栏 名称、和 文章did
		String name = (String) map.get("name");
//		String id = (String) map.get("id");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id"); 
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 执行专栏删除方法
			boolean result = this.columnService.deleteDocumentByColumn(uid, name);
			System.out.println("result测试："+result);
			if(result != false){
				msg = "专栏删除成功";
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}else{
				msg = "删除失败,当前专栏不存在";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能六、用户取消专栏下的文章收藏
	 */
	@RequestMapping(value="/c/deletecolumnbydocument" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject deletecolumnbydocument(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "文章取消收藏失败";
		int status = 1;
		// 获取当前需要更改的专栏 名称、和 文章did
		String name = (String) map.get("name");
		String did = (String) map.get("did");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id"); 
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 查询当前专栏下的文章数量
			List<YgColumn> column = this.columnService.findUserByColumn(name);
			System.out.println("当前专栏的文章数量："+column.size());
			// 当该专栏只有一篇文章时，不能直接执行删除操作
			if( column.size() > 1 ){
				// 执行专栏删除方法
				boolean result = this.columnService.deleteColumnByDocument(uid, name, did);
				System.out.println("result测试："+result);
				if(result != false){
					msg = "文章取消收藏成功";
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}else{
					msg = "文章取消收藏失败";
					status = -1;
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}
			}else{
				// 执行专栏 删除方法 并  添加方法
				boolean resultTwo = this.columnService.deleteColumnByDocument(uid, name, did);
				System.out.println("resultTwo测试："+resultTwo);
				if(resultTwo != false){
					// 执行添加方法
					boolean results = this.columnService.addColumn(uid,name);
					if(results != false){
						msg = "文章取消收藏成功";
						json.put("msg", msg);
						json.put("status", status);
						return json;
					}else{
						msg = "文章添加失败";
						status = -1;
						json.put("msg", msg);
						json.put("status", status);
						return json;
					}
				}else{
					msg = "文章取消收藏失败";
					status = -1;
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能七、用户收藏某篇文章 - 》 查询全部专栏，选择文章放入的专栏
	 */
	@RequestMapping(value="/c/allcolumn" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject AllColumn(HttpServletRequest request){
		// 测试map
//		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "专栏查询失败";
		int status = 1;
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id");
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 执行专栏查询方法
			List<YgColumn> column = this.usersService.AllColumn(uid);
			System.out.println("测试list——column:"+column);
			if(column != null){
				// 返回当前用户基本信息给前端,将信息封装进map集合中
//				Map<String,Object> users = new HashMap<>();
//				users.put("nickname",user.getNickname());
//				users.put("ygColumn",user.getYgColumn());
				// 通过Set集合无序、不可重复 进行 筛选专栏名称重复的name值
//				Set<YgColumn> sets = new HashSet<YgColumn>();
//				for(YgColumn columns : column){
//					sets.add(columns);
//				}
				msg = "用户专栏查询成功";
				json.put("msg", msg);
				json.put("column",column);
				json.put("status", status);
				return json;
			}else{
				msg = "用户专栏查询失败";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能七、用户收藏某篇文章 - 》 执行文章收藏功能
	 */
	@RequestMapping(value="/c/addcolumnbydocument" , method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject addcolumnbydocument(@RequestBody Map<String,Object> map,HttpServletRequest request){
		// 测试map
		System.out.println("map测试："+map);
		// 生成json串
		JSONObject json = new JSONObject();
		String msg = "文章收藏失败";
		int status = 1;
		// 获取当前需要的专栏 名称、和 文章 did
		String name = (String) map.get("name");
		String did = (String) map.get("did");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id"); 
		// 判断当前身份是否正确
		if("user".equals(role)){
			// 判断当前用户专栏下是否已收藏当前文章，不可在一个专栏下重复收藏同一文章
			YgColumn column = this.columnService.findColumnBydid(uid, name, did);
			System.out.println("column测试："+column);
			if(column != null){
				msg = "不可重复收藏该文章";
				status = -1;
				json.put("msg", msg);
				json.put("status", status);
				return json;
			} else {
				// 执行文章收藏方法
				boolean result = this.columnService.addDocumentByColumn(uid, name, did);
				System.out.println("result测试：" + result);
				if (result != false) {
					msg = "文章收藏成功";
					json.put("msg", msg);
					json.put("status", status);
					return json;
				} else {
					msg = "文章收藏失败";
					status = -1;
					json.put("msg", msg);
					json.put("status", status);
					return json;
				}	
			}
		}else{
			status = -1;
			json.put("msg", msg);
			json.put("status", status);
			return json;
		}
	}
	
	/*
	 *  移动端：专栏模块功能八、修改专栏名称 
	 */
	@RequestMapping(value="/c/updatecolumnname", method = RequestMethod.POST ,produces = "application/json;charset=UTF-8" )
	@ResponseBody
	public JSONObject updatecolumnname(@RequestBody Map<String,Object> map,HttpServletRequest request){
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "专栏名称修改失败";
		String name = (String) map.get("name");
		String new_name = (String) map.get("new_name");
		// 获取当前用户Token
		String token = request.getHeader("Token");
		String role = JWTUtil.getClaim(token, "role");
		String uid = JWTUtil.getClaim(token, "id");
		if("user".equals(role)){
			//判断输入的新专栏名称是否已经存在
			List<YgColumn> column = this.columnService.findColumnByName(uid,new_name);
			System.out.println("测试column："+column);
			if(column == null || column.size() == 0){
				//执行专栏名称修改方法
				boolean result = this.columnService.updateColumnByName(uid,name,new_name);
				if(result != false){
					msg = "专栏名称修改成功";
					status = 1;
					json.put("status", status);
					json.put("msg", msg);
					return json;
				}else{
					json.put("status", status);
					json.put("msg", msg);
					return json;
				}
			}else{
				msg = "当前专栏名称已存在";
				json.put("status", status);
				json.put("msg", msg);
				return json;
			}
		}else{
			msg = "当前用户未登录";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		
		
	}
	
	
}
